package com.example.demo.game;

import com.example.demo.model.Userinfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import javax.jws.soap.SOAPBinding;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.UUID;

/**
 * 匹配详情
 */
@Component
public class Matcher {
    private Queue<Userinfo> queue1 = new LinkedList<>();//1000-2000
    private Queue<Userinfo> queue2 = new LinkedList<>();//2000-3000
    private Queue<Userinfo> queue3 = new LinkedList<>();//3000以上
    private Queue<Userinfo> queue4 = new LinkedList<>();//人机对战用户的匹配队列

    @Autowired
    private GetUserLoginMatch getUserLoginMatch;

    private ObjectMapper objectMapper =new ObjectMapper(); //json序列化

    @Autowired
    private RoomManager roomManager;//房间管理器

//    /**
//     * 增加人机匹配队列中的人
//     * @param userinfo
//     */
//    public void addMachine(Userinfo userinfo) {
//        synchronized (queue4) {
//            queue4.offer(userinfo);
//            System.out.println("加入人机队列成功");
//        }
//    }
    /**
     * 增加匹配队列中的人
     */
    public void addMatcher(Userinfo userinfo) {
        int score=userinfo.getScore();
        if(score<2000) {//按天梯分排行
            synchronized (queue1) {
                queue1.offer(userinfo);
                queue1.notify();
                System.out.println("加入队列成功");
            }
        }else if(score>=2000&&score<3000) {
            synchronized (queue2) {
                queue2.offer(userinfo);
                queue2.notify();
                System.out.println("加入队列成功");
            }
        }else {
            synchronized (queue3) {
                queue3.offer(userinfo);
                queue3.notify();
                System.out.println("加入队列成功");
            }
        }
    }

    /**
     * 删除匹配队列中的人
     */
    public void removeMather(Userinfo userinfo) {
        int score=userinfo.getScore();
        if(score>=1000&&score<2000) {//按天梯分排行
            synchronized (queue1) {//保护线程安全
                queue1.remove(userinfo);
                System.out.println("取消成功");
            }
        }else if(score>=2000&&score<3000) {
            synchronized (queue2) {
                queue2.remove(userinfo);
                System.out.println("取消成功");
            }
        }else {
            synchronized (queue3) {
                queue3.remove(userinfo);
                System.out.println("取消成功");
            }
        }
    }

    public Matcher() {
        Thread t1 =new Thread() {
            @Override
            public void run() {
                while(true) {
                    try {
                        fun(queue1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //匹配的具体方法
                }
            }
        };
        t1.start();

        Thread t2 =new Thread() {
            @Override
            public void run() {
                while(true) {
                    try {
                        fun(queue2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //匹配的具体方法
                }
            }
        };
        t2.start();

        Thread t3 =new Thread() {
            @Override
            public void run() {
                while(true) {
                    try {
                        fun(queue3);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //匹配的具体方法
                }
            }
        };
        t3.start();
    }

    private void fun(Queue<Userinfo> queue) throws IOException, InterruptedException {
        synchronized (queue) {
            while (queue.size()<2) {
                queue.wait();
            }

            //1.获取匹配队列前两个人
            Userinfo userinfo1= queue.poll();
            System.out.println("匹配队列一为:"+userinfo1.toString());
            Userinfo userinfo2=queue.poll();
            System.out.println("匹配队列二为:"+userinfo2.toString());

            //2.获取这两个人的会话信息
            WebSocketSession session= getUserLoginMatch.getFromGameHall(userinfo1.getUserid());
            WebSocketSession session1= getUserLoginMatch.getFromGameHall(userinfo2.getUserid());
            //用户1掉线把用户2加回去
            if(session==null) {
                queue.offer(userinfo2);
                return;
            }
            //用户2掉线把用户1加回去
            if(session1==null) {
                queue.offer(userinfo1);
                return;
            }
            if(session==session1) {
                queue.offer(userinfo1);
                return;
            }
            //3.TODO: 将两个玩家共同加载到游戏房间
            Room room =new Room();//一个房间
            //先不设置房间玩家,等视频

            roomManager.add(room,userinfo1,userinfo2);
            System.out.println("将玩家加入房间中");
            //4.将匹配到的响应返回去给前端
            RespMatch respMatch =new RespMatch();
            respMatch.setOk(true);
            respMatch.setMessage("matchsuccess");
            session.sendMessage(new TextMessage(objectMapper.writeValueAsString(respMatch)));
            session1.sendMessage(new TextMessage(objectMapper.writeValueAsString(respMatch)));
        }
    }
}
